package com.example.websocketstompdemo.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.WebSocketHandlerDecorator;
import org.springframework.web.socket.handler.WebSocketHandlerDecoratorFactory;

@Slf4j
@Component
public class MyWebSocketHandlerDecoratorFactory implements WebSocketHandlerDecoratorFactory {
    @Override
    public WebSocketHandler decorate(WebSocketHandler handler) {
        return new WebSocketHandlerDecorator(handler){
            /**
             * 当客户端建立连接时候
             * @param session
             * @throws Exception
             */
            @Override
            public void afterConnectionEstablished(WebSocketSession session) throws Exception {
                String name = session.getUri().getPath();
                log.info(name+"上线");
                super.afterConnectionEstablished(session);
            }

            /**
             * 当客户端断开连接的时候
             * @param session
             * @param closeStatus
             * @throws Exception
             */
            @Override
            public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
                String name = session.getUri().getPath();
                log.info(name+"下线");
                super.afterConnectionClosed(session, closeStatus);
            }
        };
    }
}
